/*
 * Em desenvolvimento!!!
 * 
 */
package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import model.Imovel;
import model.Proprietario;

/**
 *
 * @author Elizangela
 */
public class ImovelDao {
    private Conexao con;
    private Statement stmt;
    private boolean sucesso = false;
    
    public ImovelDao(){
        con = new Conexao();
        try{
            stmt=(Statement)con.getCon().createStatement();
        }catch(SQLException ex){
            ex.printStackTrace();
        }
    } 
    
        public boolean inserir(Imovel imovel) {
            String inserirImovel=("INSERT INTO imovel (opcao,tipo,areaUtil,areaTotal,numQuartos,numSuites,numGaragens,"
                    + "valor,cidade,estado,endereco,bairro,obs,codDono, foto,caminho) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        try {
            PreparedStatement pstmt = con.getCon().prepareStatement(inserirImovel);
            pstmt.setString(1,imovel.getOpcao());
            pstmt.setString(2,imovel.getTipo());
            pstmt.setInt(3,imovel.getAreaUtil());
            pstmt.setInt(4,imovel.getAreaTotal());
            pstmt.setInt(5,imovel.getNumQuartos());
            pstmt.setInt(6,imovel.getNumSuites());
            pstmt.setInt(7,imovel.getNumGaragens());
            pstmt.setInt(8,imovel.getValor());
            pstmt.setString(9,imovel.getCidade());
            pstmt.setString(10,imovel.getEstado());
            pstmt.setString(11,imovel.getEndereco());
            pstmt.setString(12,imovel.getBairro());
            pstmt.setString(13,imovel.getObs());
            pstmt.setInt(14,imovel.getDono().getCodDono());
            pstmt.setBytes(15,imovel.getFoto());
            pstmt.setString(16,imovel.getCaminho());
            pstmt.executeUpdate();
            sucesso = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.fecharConexao();
        }

        return sucesso;
    }

    public List<Imovel> listar() {
       List<Imovel> imoveis = new ArrayList<Imovel>();
        int codDono;
        try {
            ResultSet rs = stmt.executeQuery("SELECT * FROM imovel ORDER BY codImovel");
            while (rs.next()) {
                Imovel imovel = new Imovel(); 
                imovel.setCodImovel(rs.getInt("codImovel"));
                imovel.setOpcao(rs.getString("opcao"));
                imovel.setTipo(rs.getString("tipo"));
                imovel.setAreaUtil(rs.getInt("areaUtil"));
                imovel.setAreaTotal(rs.getInt("areaTotal"));
                imovel.setNumQuartos(rs.getInt("numQuartos"));
                imovel.setNumSuites(rs.getInt("numSuites"));
                imovel.setNumGaragens(rs.getInt("numGaragens"));
                imovel.setValor(rs.getInt("valor"));
                imovel.setCidade(rs.getString("cidade"));
                imovel.setEstado(rs.getString("estado"));
                imovel.setEndereco(rs.getString("endereco"));
                imovel.setBairro(rs.getString("bairro"));
                imovel.setObs(rs.getString("obs"));                
                codDono=rs.getInt("codDono");                
                Proprietario prop=new Proprietario();
                prop.setCodDono(codDono);
                imovel.setDono(prop);
                imovel.setFoto(rs.getBytes("foto"));
                imovel.setCaminho(rs.getString("caminho"));
                imoveis.add(imovel);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.fecharConexao();
        }
        return imoveis;
    }

    
    public Imovel selecionar(int codImovel) {
        Imovel imovel = new Imovel();                
        int codDono;
        try {
            ResultSet rs = stmt.executeQuery("SELECT * FROM imovel where codImovel= '"+codImovel+"'");
            while (rs.next()) {
                imovel.setOpcao(rs.getString("opcao"));
                imovel.setTipo(rs.getString("tipo"));
                imovel.setAreaUtil(rs.getInt("areaUtil"));
                imovel.setAreaTotal(rs.getInt("areaTotal"));
                imovel.setNumQuartos(rs.getInt("numQuartos"));
                imovel.setNumSuites(rs.getInt("numSuites"));
                imovel.setNumGaragens(rs.getInt("numGaragens"));
                imovel.setValor(rs.getInt("valor"));
                imovel.setCidade(rs.getString("cidade"));
                imovel.setEstado(rs.getString("estado"));
                imovel.setEndereco(rs.getString("endereco"));
                imovel.setBairro(rs.getString("bairro"));
                imovel.setObs(rs.getString("obs"));                
                codDono=rs.getInt("codDono");
                Proprietario prop=new Proprietario();
                prop.setCodDono(codDono);
                imovel.setDono(prop);
                imovel.setFoto(rs.getBytes("foto"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.fecharConexao();
        }
        return imovel;
    }


    public boolean alterar(Imovel imovel) {
        String alterarImovel=("UPDATE imovel SET opcao=?,tipo=?,areaUtil=?,areaTotal=?,numQuartos=?,numSuites=?,"
                + "numGaragens=?,valor=?,cidade=?,estado=?,endereco=?,bairro=?,obs=?,codDono=?,foto=? WHERE codImovel=?");
        try {
            PreparedStatement pstmt = con.getCon().prepareStatement(alterarImovel);
            pstmt.setString(1,imovel.getOpcao());
            pstmt.setString(2,imovel.getTipo());
            pstmt.setInt(3,imovel.getAreaUtil());
            pstmt.setInt(4,imovel.getAreaTotal());
            pstmt.setInt(5,imovel.getNumQuartos());
            pstmt.setInt(6,imovel.getNumSuites());
            pstmt.setInt(7,imovel.getNumGaragens());
            pstmt.setInt(8,imovel.getValor());
            pstmt.setString(9,imovel.getCidade());
            pstmt.setString(10,imovel.getEstado());
            pstmt.setString(11,imovel.getEndereco());
            pstmt.setString(12,imovel.getBairro());
            pstmt.setString(13,imovel.getObs());
            pstmt.setInt(14,imovel.getDono().getCodDono());
            pstmt.setBytes(15,imovel.getFoto());
            pstmt.setInt(16, imovel.getCodImovel());
            pstmt.executeUpdate();
            sucesso = true;
           
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.fecharConexao();
        }

        return sucesso;
    }

    public boolean remover(Imovel imovel) {
        try {
            stmt.execute("DELETE FROM imovel WHERE codImovel = '" +imovel.getCodImovel()+ "'");
            sucesso = true;            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.fecharConexao();
        }
        return sucesso;
    }
    
    
   
    
}
